<template>
  <div class="goodDetail">
    <swiper :swipers="swipers"></swiper>
    <div class="description">
      <h2>{{goods.title}}</h2>
      <div class="price">
        <span>现价¥{{goods.sell_price}}</span>
        <span>原价{{goods.market_price}}</span>
      </div>
      <div class="num">
        <span>购买数量:</span>
        <van-stepper v-model="value" />
      </div>
      <div class="btn">
        <van-button
          class="btn1"
          type="info"
        >立即购买</van-button>
        <van-button
          type="danger"
          @click="addCar"
        >加入购物车</van-button>
      </div>
    </div>
    <div class="goods_msg">
      <h2>{{goods.title}}</h2>
      <p>商品情况:{{goods.goods_no}}件</p>
      <p>库存情况:{{goods.stock_quantity}}件</p>
      <p>上架时间:{{goods.add_time}}</p>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import swiper from '../../../../comment/swiper/swiper.vue'
import { getThumimages, getgoodsMsg } from '@/api/home.js'
export default {
  props: ['id'],
  data: () => ({
    swipers: [],
    value: 2,
    goods: []
  }),
  created() {
    this.getThumimages()
    this.getgoodsMsg()
  },
  methods: {
    async getThumimages() {
      const res = await getThumimages(this.id)
      this.swipers = res.data.message
    },
    async getgoodsMsg() {
      const res = await getgoodsMsg(this.id)
      this.goods = res.data.message
    },
    addCar() {
      const goodsCar = {
        id: this.goods.id,
        price: this.goods.sell_price,
        title: this.goods.title,
        imgSrc: this.swipers[0].src,
        num: this.value,
        select: true, //购物车中商品的选中按钮,默认选中状态
        time: moment().format('YYYY-MM-DD-HH-MM-SS')
      }
      this.$store.commit({
        type: 'addCar',
        goodsCar
      })
    }
  },
  components: {
    swiper
  }
}
</script>
<style lang="less" >
.goodDetail {
  font-family: "楷体";
  padding: 10px;
  .my-swipe .van-swipe-item {
    height: 200px;
    line-height: 200px;
    text-align: center;
    img {
      width: 40%;
    }
  }
  .van-grid-item__icon {
    font-size: 40px;
  }
  .description {
    border: 1px solid lightgray;
    padding-left: 10px;
    padding-bottom: 10px;
    .price {
      margin-bottom: 10px;
      span:nth-child(1) {
        margin-right: 20px;
        color: red;
      }
      span:nth-child(2) {
        text-decoration: line-through;
      }
    }
    .num {
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      span {
        margin-right: 15px;
      }
    }
    .btn1 {
      margin-right: 20px;
    }
  }
  .goods_msg {
    margin-top: 5px;
    border: 0.5px solid lightgray;
    padding-left: 10px;
  }
}
</style>